
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "types.h"
#include "y.tab.h"

void commentaire_slash();
void commentaire_etoile();

int gLineNo = 1;

%}


%%

"int"	      {return tINT;}
"const"     {return tCONSTANT;}
"void"      {return tVOID;}
"while"     {return tWHILE;}
"for"		   {return tFOR;}
"if"        {return tIF;}
"else"      {return tELSE;}
"printf"    {return tPRINTF;}
"return"    {return tRETURN;}
"break"     {return tBREAK;}
"continue"  {return tCONTINUE;}

[a-zA-Z_][a-zA-Z0-9_]*  {
                           yylval.name = strdup(yytext);
                           return tID_VARIABLE;
                        }
[0-9]+	{
            yylval.val = atoi(yytext);
            return tNUMBER;
         }
"&"		{return '&';}
"{"	   {return '{';}
"}"	   {return '}';}
"("	   {return '(';}
")"	   {return ')';}
"["      {return '[';}
"]"      {return ']';}
";"	   {return ';';}
","	   {return ',';}
"="	   {return '=';}
"+"	   {return '+';}
"-"	   {return '-';}
"*"	   {return '*';}
"/"	   {return '/';}
"%"		{return '%';}
">"	   {return '>';}
"<"	   {return '<';}
"+="		{return tPLUSEGAL; }
"-="		{return tMOINSEGAL;}
"*="		{return tMULEGAL;}
"/="		{return tDIVEGAL;}
"++"		{return tPLUSPLUS;}
"--"		{return tMOINSMOINS;}
">="	   {return tGE;}
"<="	   {return tLE;}
"!="	   {return tNOT_EQUAL;}
"=="	   {return tEQUAL;}
"//"	   {commentaire_slash();}
"/*"	   {commentaire_etoile();}
[ \t]  	{}
"\n"		{gLineNo++;}
.        {/*printf("unexpected character");*/}

%%

void commentaire_slash(){
	int c ;
	while ((c = input()) != '\n' && c != 0){}
	gLineNo++;
}

void commentaire_etoile()
{
	char c, c1;
loop:
	while ((c = input()) != '*' && c != 0) {
		putchar(c);
		if (c == '\n')
			gLineNo++;
	}

	if ((c1 = input()) != '/' && c != 0)
	{
		unput(c1);
		goto loop;
	}

	if (c != 0) {
		putchar(c1);
		if (c1 == '\n') 
			gLineNo++;
	}
}
